package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.dms.databinding.ActivityAnnouncementPushBinding;
import com.student.dms.databinding.ActivityChangePasswordBinding;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ChangePasswordActivity extends BaseActivity{

    private  ActivityChangePasswordBinding activityChangePasswordBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityChangePasswordBinding = ActivityChangePasswordBinding.inflate(getLayoutInflater());
        setContentView(activityChangePasswordBinding.getRoot());
        activityChangePasswordBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityChangePasswordBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editPassword = activityChangePasswordBinding.editPassword.getText().toString().trim();
                String editConfirmPassword = activityChangePasswordBinding.editConfirmPassword.getText().toString().trim();
                if(checkInputIsNull(editPassword) || checkInputIsNull(editConfirmPassword)){
                    ToastUtils.showToast(ChangePasswordActivity.this, "密码或确认密码输入不能为空！");
                }else if(!TextUtils.equals(editPassword,editConfirmPassword)){
                    ToastUtils.showToast(ChangePasswordActivity.this, "新密码需要跟确认新密码相同！");
                }else if(!checkPassword(editPassword)){
                    ToastUtils.showToast(ChangePasswordActivity.this, "请输入8-12位密码，必须包含大小写字母，数字和特殊字符！");
                }else{
                    SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                    String username = sharedPreferences.getString("username", "");
                    if(!TextUtils.isEmpty(username)){
                        AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
                        AccountDao accountDao = roomDatabase.provideAccountDao();
                        Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
                        disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                            accountEntity.setPassWord(editPassword);
                            disposable.add(accountDao.insert(accountEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() ->{
                                ToastUtils.showToast(ChangePasswordActivity.this, "密码更新成功！");
                                ChangePasswordActivity.this.finish();
                            } , insertstudentDaothrowable -> ToastUtils.showToast(ChangePasswordActivity.this, "密码更新失败！  message: " + insertstudentDaothrowable.getMessage())));
                        }, studentFlowThrowable -> {
                            ToastUtils.showToast(ChangePasswordActivity.this, "用户不存在，请注册！");
                        }));
                    }

                }
            }
        });
    }

    public boolean checkPassword(String password) {
        // 定义正则表达式，用于验证密码格式
        String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_]).{8,12}$";
        // 编译正则表达式为模式对象
        Pattern pattern = Pattern.compile(regex);
        // 创建匹配器对象，用于匹配输入的密码字符串
        Matcher matcher = pattern.matcher(password);
        // 通过匹配器的matches方法判断是否匹配成功，返回结果
        return matcher.matches();
    }
    private boolean checkInputIsNull(String text) {
        return TextUtils.isEmpty(text);
    }
}
